package ch11;

import java.util.ArrayList; 
import java.util.LinkedList;
import java.util.List;

public class _05_ArrayListLinkedListTest { 
      public static void main(String args[]) {            
            ArrayList<String> al = new ArrayList<String>(10000000);
            LinkedList<String> ll = new LinkedList<String>(); 
            
            // squential add
            System.out.println("ArrayList :"+add1(al)+"ms"+" ,LinkedList :"+add1(ll)+"ms"); 
            // ArrayList :866ms ,LinkedList :1615ms

            // randomly add
            System.out.println("ArrayList :"+add2(al)+"ms"+" ,LinkedList :"+add2(ll)+"ms"); 
            // ArrayList :1155ms ,LinkedList :6ms
            
            // remove from first to last
            System.out.println("ArrayList :"+remove1(al)+"ms"+" ,LinkedList :"+remove1(ll)+"ms"); 
            // ArrayList :1159ms ,LinkedList :2ms
            
            // remove last from to first
            System.out.println("ArrayList :"+remove2(al)+"ms"+"   ,LinkedList :"+remove2(ll)+"ms"); 
            // ArrayList :12ms   ,LinkedList :30ms
      } 

      static long add1(List<String> list) { 
            long start = System.currentTimeMillis(); 
            for(int i=0; i<1000000;i++) list.add(i+""); 
            long end = System.currentTimeMillis(); 
            return end - start; 
      } 

      static long add2(List<String> list) { 
            long start = System.currentTimeMillis(); 
            for(int i=0; i<1000;i++) list.add(500, "X"); 
            long end = System.currentTimeMillis(); 
            return end - start; 
      }        

      static long remove1(List<String> list) { 
            long start = System.currentTimeMillis(); 
            for(int i=0; i<1000;i++) list.remove(i); 
            long end = System.currentTimeMillis(); 
            return end - start; 
      }
      
      static long remove2(List<String> list) { 
          long start = System.currentTimeMillis(); 
          for(int i=list.size()-1; i > 0;i--) list.remove(i); 
          long end = System.currentTimeMillis(); 
          return end - start; 
    }
} 